package cn.math;

import java.util.ArrayList;
import java.util.List;
import java.util.OptionalDouble;

public class MyStatistics {

    /**
     * 计算方差 Variance
     * */
    public static Double sampleVariance(List<Double> lst) {
        double sampleVariance = calculateSampleVariance(lst);
        return sampleVariance;
    }

    public static Double sampleVariance_f(List<Float> lst) {
        List<Double> rs = new ArrayList<>();
        for (Float m : lst) {
            rs.add(Double.valueOf(m));
        }
        double sampleVariance = calculateSampleVariance(rs);
        return sampleVariance;
    }

    public static Double mean_f(List<Float> lst) {
        List<Double> rs = new ArrayList<>();
        for (Float m : lst) {
            rs.add(Double.valueOf(m));
        }
        double sum = rs.stream().mapToDouble(Double::doubleValue).sum();
        return sum;
    }

    public static Double my_mean_f(List<Float> lst) {
        List<Double> rs = new ArrayList<>();
        for (Float m : lst) {
            rs.add(Double.valueOf(m));
        }
        double sum = rs.stream().mapToDouble(Double::doubleValue).sum();
        double sampleVariance = calculateSampleVariance(rs);
        return (sampleVariance / (sum / lst.size())) * 100;
    }

    public static Double my_mean(List<Double> lst) {
        double sum = lst.stream().mapToDouble(Double::doubleValue).sum();
        double sampleVariance = calculateSampleVariance(lst);
        return (sampleVariance / (sum / lst.size())) * 100;
    }

    // 计算均值
    public static double calculateMean(List<Double> data) {
        if (data == null || data.isEmpty()) {
            throw new IllegalArgumentException("数据列表不能为空");
        }

        // 转换为 DoubleStream 并计算均值
        OptionalDouble average = data.stream()
                .mapToDouble(Double::doubleValue) // 转换为原始 double 类型
                .average();                       // 计算均值，返回 OptionalDouble

        // 处理结果
        return average.orElseThrow(() ->
                new IllegalArgumentException("无法计算空列表的均值")
        );

    }

    // 计算总体方差（公式：sum(x²)/n - (sum(x)/n)²）
    public static double calculatePopulationVariance(List<Double> data) {
        if (data == null || data.isEmpty()) {
            throw new IllegalArgumentException("数据列表不能为空");
        }
        double sum = data.stream().mapToDouble(Double::doubleValue).sum();
        double sumOfSquares = data.stream().mapToDouble(x -> x * x).sum();
        long count = data.size();
        return (sumOfSquares - (sum * sum) / count) / count;
    }

    // 计算样本方差（公式：(sum(x²) - (sum(x)²)/n) / (n-1)）
    public static double calculateSampleVariance(List<Double> data) {
        if (data == null || data.size() < 2) {
            throw new IllegalArgumentException("数据列表至少需要两个元素");
        }
        double sum = data.stream().mapToDouble(Double::doubleValue).sum();
        double sumOfSquares = data.stream().mapToDouble(x -> x * x).sum();
        long count = data.size();
        return (sumOfSquares - (sum * sum) / count) / (count - 1);
    }
}
